package com.fangyi.yunmusic.service.impl;

import com.fangyi.yunmusic.exception.YunMusicException;
import com.fangyi.yunmusic.mapper.ArtistMapper;
import com.fangyi.yunmusic.mapper.PlaylistMapper;
import com.fangyi.yunmusic.mapper.SubscribeMapper;
import com.fangyi.yunmusic.mapper.UserMapper;
import com.fangyi.yunmusic.pojo.Subscribe;
import com.fangyi.yunmusic.service.CollectionService;
import org.n3r.idworker.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import static com.fangyi.yunmusic.constant.CollectionTypeConstant.ARTIST;
import static com.fangyi.yunmusic.constant.CollectionTypeConstant.SONGLIST;

@Service
public class CollectionServiceImpl implements CollectionService {


    @Autowired
    private SubscribeMapper subscribeMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArtistMapper artistMapper;
    @Autowired
    private PlaylistMapper playlistMapper;
    @Override
    public void collectPlaylist(String playlistId, String userId) {
        if(userMapper.selectByPrimaryKey(userId)==null)
            throw new YunMusicException("用户id不存在！");
        if(playlistMapper.selectByPrimaryKey(playlistId)==null)
            throw new YunMusicException("歌单id不存在！");
        if(isCollected(userId,playlistId,SONGLIST))
            throw new YunMusicException("歌单已收藏！");
        addCollection(playlistId,userId,SONGLIST);
//        Subscribe subscribe=new Subscribe();
//        subscribe.setId(Id.nextString());
//        subscribe.setType(SONGLIST);//收藏类型。0：收藏歌单；1：收藏歌手
//        subscribe.setTargetId(playlistId);
//        subscribe.setUserId(userId);
//        subscribeMapper.insert(subscribe);
    }

    @Override
    public void collectArtist(String artistId, String userId) {
        if(userMapper.selectByPrimaryKey(userId)==null)
            throw new YunMusicException("用户id不存在！");
        if(artistMapper.selectByPrimaryKey(artistId)==null)
            throw new YunMusicException("歌手id不存在！");
        if(isCollected(userId,artistId,ARTIST))
            throw new YunMusicException("歌手已收藏！");
        addCollection(artistId,userId,ARTIST);
//        Subscribe subscribe=new Subscribe();
//        subscribe.setId(Id.nextString());
//        subscribe.setType(ARTIST);//收藏类型。0：收藏歌单；1：收藏歌手
//        subscribe.setTargetId(artistId);
//        subscribe.setUserId(userId);
//        subscribeMapper.insert(subscribe);
    }
    private void addCollection(String targetId,String userId,Integer type){
        if(isCollected(targetId,userId,type)==true)
            throw new YunMusicException("已收藏！");
        Subscribe subscribe=new Subscribe();
        subscribe.setId(Id.nextString());
        subscribe.setType(type);//收藏类型。0：收藏歌单；1：收藏歌手
        subscribe.setTargetId(targetId);
        subscribe.setUserId(userId);
        subscribeMapper.insert(subscribe);
    }
    private void cancelCollection(String targetId,String userId,Integer type){
        Example example=new Example(Subscribe.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("userId",userId).andEqualTo("targetId",targetId).andEqualTo("type",type);
        subscribeMapper.deleteByExample(example);
    }
    private boolean isCollected(String userId,String targetId,Integer type){
        Example example=new Example(Subscribe.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("userId",userId).andEqualTo("targetId",targetId).andEqualTo("type",type);
        if(subscribeMapper.selectByExample(example).size()==1)
            return true;
        else return false;

    }
    @Override
    public void discollectPlaylist(String playlistId, String userId) {
        if(isPlaylistCollected(playlistId,userId)==false)
            throw new YunMusicException("未收藏歌单！");
        cancelCollection(playlistId,userId,SONGLIST);

    }

    @Override
    public void discollectArtist(String artistId, String userId) {
        if(isArtistCollected(artistId,userId)==false)
            throw new YunMusicException("未收藏歌手！");
        cancelCollection(artistId,userId,ARTIST);
    }

    @Override
    public Boolean isPlaylistCollected(String playlistId, String userId) {
        if(userMapper.selectByPrimaryKey(userId)==null)
            throw new YunMusicException("用户id不存在！");
        if(playlistMapper.selectByPrimaryKey(playlistId)==null)
            throw new YunMusicException("歌单id不存在！");
        return isCollected(userId,playlistId,SONGLIST);
    }

    @Override
    public Boolean isArtistCollected(String artistId, String userId) {
        if(userMapper.selectByPrimaryKey(userId)==null)
            throw new YunMusicException("用户id不存在！");
        if(artistMapper.selectByPrimaryKey(artistId)==null)
            throw new YunMusicException("歌手id不存在！");
        return isCollected(userId,artistId,ARTIST);
    }
}
